package org.ftz.control;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class util {
    public static String getHostIp() {  //获取本机ip地址不是0:0:0那个地址
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress ip = (InetAddress) addresses.nextElement();
                    if (ip != null
                            && ip instanceof Inet4Address
                            && !ip.isLoopbackAddress() //loopback地址即本机地址，IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
                            && ip.getHostAddress().indexOf(":") == -1) {
                        System.out.println("本机的IP = " + ip.getHostAddress());
                        return ip.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void writeFile(String content){  //输入数据存储到文件中
        String FilePath="D:\\BigProgram\\1.txt";
        FileWriter fileWriter=null;
        try {
            File file = new File(FilePath);
            if(!file.exists()){
                file.createNewFile();
            }
            fileWriter=new FileWriter(FilePath);
            fileWriter.write(content);
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static int getZipFileCount(String zipFilePath) {  //获取zip压缩包中文件个数
        ZipFile zf = null;
        int count = 0;
        try {
            zf = new ZipFile(zipFilePath);
            count = zf.size();     //返回zip文件中的条目数
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return count;
    }
    public static java.util.List<String> getZipFileList(String zipFileName) throws IOException { //获取zip压缩包下的所有txt目录
        String input="",output="";
        ZipInputStream zipInputStream = null;
        int fileCount = getZipFileCount(zipFileName);
        String in[]=new String[fileCount/2];
        String out[]=new String[fileCount/2];
        try (FileInputStream inputStream = new FileInputStream(zipFileName)) {
            zipInputStream = new ZipInputStream(
                    new BufferedInputStream(inputStream), StandardCharsets.UTF_8);
            java.util.zip.ZipEntry ze;
            List<String> list = new ArrayList<>();
            while ((ze = zipInputStream.getNextEntry()) != null) {  //循环遍历
                if (!ze.isDirectory()) {
                    String content = getZipOneTestFileContent(zipFileName, ze.getName());
                    System.out.println("name："+ze.getName());
                    if(ze.getName().startsWith("input")){
                        System.out.println(ze.getName());
                        String[] split = ze.getName().split("\\.");
                        int i = Integer.parseInt(split[0].substring(5));
                        in[i-1]=content;
                    }else if(ze.getName().startsWith("output")){
                        System.out.println(ze.getName());
                        String[] split = ze.getName().split("\\.");
                        int j=Integer.parseInt(split[0].substring(6));
                        out[j-1]=content;
                    }
                }
            }
            for(int i=0;i<fileCount/2;i++){
                input+=in[i]+"@";
                output+=out[i]+"@";
            }
            list.add(input);
            list.add(output);
            return list;
        } catch (Exception e) {
            return null;
        } finally {
            if (zipInputStream != null) {
                zipInputStream.closeEntry();
            }
        }
    }
    public static String getZipOneTestFileContent(String zipFileName, String fileName) throws IOException { //获取zip压缩包内一个txt文件内容
        ZipInputStream zipInputStream = null;
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(zipFileName);
            zipInputStream = new ZipInputStream(
                    new BufferedInputStream(inputStream), StandardCharsets.UTF_8);
            java.util.zip.ZipEntry ze;
            StringBuilder txtStr = new StringBuilder();
            while ((ze = zipInputStream.getNextEntry()) != null) { //循环遍历
                if (!ze.isDirectory() && ze.getName().equals(fileName)) {
                    byte[] b = new byte[1024];
                    int leng;
                    while ((leng = zipInputStream.read(b)) != -1) {
                        txtStr.append(new String(b, 0, leng));
                    }
                    break;
                }
            }
            return txtStr.toString();
        } catch (Exception e) {
            return null;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (zipInputStream != null) {
                zipInputStream.close();
            }
        }
    }
    public static boolean deleteFile(String fileName) { //删除单个文件
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {  // 如果文件路径只有单个文件
            if (file.delete()) {
                System.out.println("删除文件" + fileName + "成功！");
                return true;
            } else {
                System.out.println("删除文件" + fileName + "失败！");
                return false;
            }
        } else {
            System.out.println(fileName + "不存在！");
            return false;
        }
    }
    public static boolean deleteAllFile(String dir) { //删除所有文件
        File dirFile = new File(dir);
        if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
            System.out.println("删除文件夹失败：" + dir + "不存在！");
            return false;
        }
        boolean flag = true;
        File[] files = dirFile.listFiles(); // 删除文件夹中的所有文件包括子文件夹
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {  // 删除子文件
                flag =deleteFile(files[i].getAbsolutePath());
                if (!flag) break;
            } else if (files[i].isDirectory()) { // 删除子文件夹
                flag = deleteAllFile(files[i].getAbsolutePath());
                if (!flag) break;
            }
        }
        if (!flag) {
            System.out.println("删除文件夹失败！");
            return false;
        }
        if (dirFile.delete()) {  // 删除当前文件夹
            System.out.println("删除文件夹" + dir + "成功！");
            return true;
        } else {
            return false;
        }
    }
}
